package com.xqyq.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xqyq.model.Sign;

@Repository
public class SignDao {
	@Autowired
	private SessionFactory sessionFactory;

	public void addSign(Sign sign) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("from Sign s where s.user.use_id=? and s.match.match_id=?");
		query.setInteger(0, sign.getUser().getUse_id());
		query.setInteger(1, sign.getMatch().getMatch_id());
		if (query.uniqueResult() == null) {
			session.save(sign);
		}
	}

	public void deleteSign(int sign_id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("delete From Sign where sign_id=?");
		query.setInteger(0, sign_id);
		query.executeUpdate();
	}

	public void passSign(int sign_id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("update Sign s set s.state=1 where s.sign_id=?");
		query.setInteger(0, sign_id);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	public List<Sign> getSignListNoPrize(int match_id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("from Sign s where s.match.match_id=? and s.prize=null and s.state>0");
		query.setInteger(0, match_id);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	public List<Sign> getSignListNoCheck() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("from Sign s where s.state=0 and s.isUpload=1");
		return query.list();
	}

	@SuppressWarnings("unchecked")
	public List<Sign> getUserSignList(int user_id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Sign s where s.user.use_id=?");
		query.setInteger(0, user_id);
		return query.list();
	}

	public void removeUserPrize(int sign_id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("update Sign s set s.prize = null where s.sign_id=?");
		query.setInteger(0, sign_id);
		query.executeUpdate();
	}

	public void addUser_prize(int sign_id, int prize_id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createSQLQuery("update Sign set prize_id=? where sign_id=?");
		query.setInteger(0, prize_id);
		query.setInteger(1, sign_id);
		query.executeUpdate();
	}

	public Sign getSignByUserAndMatch(int user_id, int match_id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Sign s where s.user.use_id=? and s.match.match_id=?");
		query.setInteger(0, user_id);
		query.setInteger(1, match_id);
		return (Sign) query.uniqueResult();
	}

	public void updateSign(Sign sign) {
		Session session = sessionFactory.getCurrentSession();
		session.update(sign);
	}

}
